<?php

namespace App\Http\Controllers;

use Illuminate\Http\JsonResponse;
use Laravel\Lumen\Routing\Controller as BaseController;

class Controller extends BaseController
{
    private $success_response_format = [
        'message' => '',
        'status_code' => 200,
        'data' => []
    ];

    private $fail_response_format = [
        'message' => '',
        'status_code' => 400,
    ];

    /**
     * 返回成功JSON
     * @param $data
     * @param string $msg
     * @return JsonResponse
     */
    protected function sendSuccessResponse($data, $msg='success'){
        $this->success_response_format['message'] = $msg;
        $this->success_response_format['data'] = $data;
        return new JsonResponse($this->success_response_format, 200);
    }

    /**
     * 返回失败JSON
     * @param $status_code
     * @param $error_code
     * @param string $msg
     * @return JsonResponse
     */
    protected function sendFailResponse($status_code, $error_code, $msg=''){
        $this->fail_response_format['message'] = $msg ? $msg : $this->codeStatus($error_code);
        $this->fail_response_format['status_code'] = $error_code;
        return new JsonResponse($this->fail_response_format, $status_code);
    }

    /**
     * 错误码
     * @param $code
     * @return mixed
     */
    private function codeStatus($code){
        $msg = [
            '-1'    => '未知错误',
            // Informational 1xx
            '100' => 'Continue',
            '101' => 'Switching Protocols',
            // Success 2xx
            '200' => 'OK',
            '201' => 'Created',
            '202' => 'Accepted',
            '203' => 'Non-Authoritative Information',
            '204' => 'No Content',
            '205' => 'Reset Content',
            '206' => 'Partial Content',
            // Redirection 3xx
            '300' => 'Multiple Choices',
            '301' => 'Moved Permanently',
            '302' => 'Moved Temporarily ', // 1.1
            '303' => 'See Other',
            '304' => 'Not Modified',
            '305' => 'Use Proxy',
            // 306 is deprecated but reserved
            '307' => 'Temporary Redirect',
            // Client Error 4xx
            '400' => 'Bad Request',
            '401' => 'Unauthorized',
            '402' => 'Payment Required',
            '403' => 'Forbidden',
            '404' => 'Not Found',
            '405' => 'Method Not Allowed',
            '406' => 'Not Acceptable',
            '407' => 'Proxy Authentication Required',
            '408' => 'Request Timeout',
            '409' => 'Conflict',
            '410' => 'Gone',
            '411' => 'Length Required',
            '412' => 'Precondition Failed',
            '413' => 'Request Entity Too Large',
            '414' => 'Request-URI Too Long',
            '415' => 'Unsupported Media Type',
            '416' => 'Requested Range Not Satisfiable',
            '417' => 'Expectation Failed',
            // Server Error 5xx
            '500' => 'Internal Server Error',
            '501' => 'Not Implemented',
            '502' => 'Bad Gateway',
            '503' => 'Service Unavailable',
            '504' => 'Gateway Timeout',
            '505' => 'HTTP Version Not Supported',
            '509' => 'Bandwidth Limit Exceeded',
            // 自定义 error code
            '10001' => '参数缺失',
            '10002' => '参数格式不符',
            '10003' => '文件类型不符',
            '10004' => '昵称有误，太长或者重复',
            '10005' => '日期格式不符',
            '10006' => '手机号码格式不符',
            '10007' => '该手机号已被注册',
            '10008' => '密码必须6位',
            '10009' => '两次密码不一致',
            '10010' => '该账号已注册',
            '40000' => '请求错误',
            '40001' => '业务逻辑不合规',
            '40002' => '验证码错误或者失效',
            '40003' => '保存文件失败',
            '40004' => '余额不足',
            '40005' => '违反相关规定不能进行该操作',
            '40101' => '无权限进行该操作',
            '40102' => '登录失败，可能是密码、验证码错误，或者第三方授权失败',
            '40103' => '登录失效，请重新登陆',
            '40104' => '未注册',
            '40105' => '支付密码错误',
            '40106' => '未设置支付密码',
            '40107' => '密码被锁定',
            '40108' => 'encodingAesKey 非法',
            '40109' => 'encodingIv 非法',
            '40110' => '解密后得到的buffer非法',
            '40301' => '用户被禁用',
            '40302' => '用户不存在',
            '40400' => '该资源不存在',
            '50000' => '服务器错误',
            '50001' => '该服务暂不提供',
            '50002' => '该服务超出限额',
            '50003' => '红包不足无法购买',
            '50004' => '该服务不能重复领取',
            '50005' => '第三方服务发生错误',

        ];
        if(isset($msg[$code])){
            return $msg[$code];
        }
        return $msg['-1'];
    }
}
